package cloud.maque.module.system.service.impl;

import cloud.maque.common.core.enums.EnumStatus;
import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.common.core.request.PageRequest;
import cloud.maque.common.core.utils.TokenUserUtil;
import cloud.maque.common.utils.PageRequestUtil;
import cloud.maque.module.system.entity.SysDictItem;
import cloud.maque.module.system.entity.SysDictType;
import cloud.maque.module.system.mapper.DictItemMapper;
import cloud.maque.module.system.service.DictItemService;
import cloud.maque.module.system.service.DictTypeService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Service
public class DictItemServiceImpl extends ServiceImpl<DictItemMapper, SysDictItem> implements DictItemService {


    @Autowired
    PageRequestUtil util;

    @Autowired
    TokenUserUtil userUtil;


    @Autowired
    DictTypeService dictTypeService;

    @Override
    public IPage<SysDictItem> GetList(PageRequest<SysDictItem> pageReq) {
        Page<SysDictItem> page = util.GetPage(SysDictItem.class, pageReq);
        QueryWrapper<SysDictItem> query = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getItemLabel())) {
            query.lambda().like(SysDictItem::getItemLabel, pageReq.getData().getItemLabel());
        }
        if (!StrUtil.isEmptyIfStr(pageReq.getData().getItemValue())) {
            query.lambda().like(SysDictItem::getItemValue, pageReq.getData().getItemValue());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getDictId())) {
            query.lambda().eq(SysDictItem::getDictId, pageReq.getData().getDictId());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getParentId())) {
            query.lambda().eq(SysDictItem::getParentId, pageReq.getData().getParentId());
        }

        query.lambda().orderByDesc(SysDictItem::getCreateTime);
        return this.page(page, query);
    }

    @Override
    public SysDictItem SaveDictItme(SysDictItem data) {
        Assert.hasText(data.getItemLabel(), "字典项名称不能为空");
        Assert.hasText(data.getItemValue(), "字典项值不能为空");
        Assert.hasText(data.getDictId(), "字典Id不能为空");

        if (StrUtil.isEmptyIfStr(data.getDictId())) {
            data.setCreateBy(userUtil.GetUserName());
            data.setCreateTime(new Date());
            data.setUpdateBy(data.getCreateBy());
            data.setUpdateTime(new Date());
        } else {
            SysDictItem oldPost = this.baseMapper.selectById(data.getDictId());
            if (oldPost != null) {
                data.setCreateTime(oldPost.getCreateTime());
                data.setCreateBy(oldPost.getCreateBy());
            }
            data.setUpdateBy(userUtil.GetUserName());
            data.setUpdateTime(new Date());

        }

        SysDictItem oldPost = GetItem(data.getDictId(), data.getItemValue());
        if (oldPost != null && !oldPost.getItemId().equals(data.getItemId())) {
            throw new BusinessException("字典项值已存在");
        }

        this.saveOrUpdate(data);
        return data;
    }

    @Override
    public Boolean DeleteItme(String itemId) {
        return this.removeById(itemId);
    }

    @Override
    public SysDictItem GetItem(String dictId, String itemValue) {
        QueryWrapper<SysDictItem> query = new QueryWrapper<>();
        query.lambda().eq(SysDictItem::getDictId, dictId);
        query.lambda().eq(SysDictItem::getItemValue, itemValue);
        return this.getOne(query);
    }


    @Override
    public SysDictItem GetItemEnabled(String dictId, String itemValue) {
        QueryWrapper<SysDictItem> query = new QueryWrapper<>();
        query.lambda().eq(SysDictItem::getDictId, dictId);
        query.lambda().eq(SysDictItem::getItemValue, itemValue);
        query.lambda().eq(SysDictItem::getStatus, EnumStatus.ENABLE.getCode());
        return this.getOne(query);
    }

    /**
     * 根据字典编码获取字典
     *
     * @param dictCode
     * @param itemValue
     * @return
     */
    @Override
    public SysDictItem GetItemEnabledByCode(String dictCode, String itemValue) {
        SysDictType dictType = dictTypeService.GetByCode(dictCode);
        if (dictType == null) {
            return null;
        }

        return this.GetItemEnabled(dictType.getDictId(), itemValue);
    }

    @Override
    public List<SysDictItem> GetItemList(String dictId) {
        QueryWrapper<SysDictItem> query = new QueryWrapper<>();
        query.lambda().eq(SysDictItem::getDictId, dictId);
        query.lambda().orderByDesc(SysDictItem::getCreateTime);
        return this.list(query);
    }


    @Override
    public List<SysDictItem> GetItemListEnabled(String dictId) {
        QueryWrapper<SysDictItem> query = new QueryWrapper<>();
        query.lambda().eq(SysDictItem::getDictId, dictId);
        query.lambda().eq(SysDictItem::getStatus, EnumStatus.ENABLE.getCode());
        query.lambda().orderByDesc(SysDictItem::getCreateTime);
        return this.list(query);
    }

    /**
     * 根据字典编码获取字典列表
     *
     * @param dictCode
     * @return
     */
    @Override
    public List<SysDictItem> GetItemListEnabledByCode(String dictCode) {

        SysDictType dictType = dictTypeService.GetByCode(dictCode);
        if (dictType == null) {
            return null;
        }
        return this.GetItemListEnabled(dictType.getDictId());
    }


}
